package myJs

import myJs.Utils.g
import myJs.implicits.Implicits._
import myJs.myPkg.Implicits._
import myJs.myPkg.jquery._
import myJs.myPkg.{ColumnOptions, ExportOptions, TableOptions}
import org.scalajs.dom.ext.Ajax

import scala.concurrent.ExecutionContext.Implicits.global
import scala.scalajs.js
import scala.scalajs.js.JSConverters._
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}


/**
 * Created by yz on 11/5/2020
 */
@JSExportTopLevel("Help")
object Help {

  val tableId = "table"

  @JSExport("init")
  def init = {
    initTable
    refreshTable

  }

  def refreshTable = {
    refreshColumnTable
  }

  def refreshColumnTable = {
    val url = g.jsRoutes.controllers.HelpController.getFileData().url.toString
    Ajax.get(url = s"${url}?fileName=columns.xlsx").map { xhr =>
      val data = xhr.responseText.toJArJDS
      $(s"#table").bootstrapTable("load", data)
    }
  }

  def tbFmt(columnName: String): js.Function = (v: js.Any, row: js.Dictionary[js.Any]) => columnName match {
    case _ => v
  }

  def initColumnTable = {
    val columnNames = List("Column", "Description")
    val columns = columnNames.map { columnName =>
      val title = columnName
      val fmt = tbFmt(columnName)
      val basicOptions = ColumnOptions.field(columnName).title(title).formatter(fmt)
      columnName match {
        case _ => basicOptions
      }
    }
    val options = TableOptions.columns(columns.toJSArray).pagination(false)
    $(s"#${tableId}").bootstrapTable(options)
    bindEvt
  }

  def initTable = {
    initColumnTable
  }

  def bindEvt = {

  }


}
